package com.sdp.wx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.cache.clean.WxCleanCache;
import com.sdp.wx.db.wx_army.entity.WxArmy;
import com.sdp.wx.db.wx_army.service.IWxArmyService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-05
 */
@RestController
@RequestMapping("/WxArmyController")
public class WxArmyController {
	
	@Autowired
	private Query query;
	
	@Autowired
	private IWxArmyService iWxArmyService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private WxCleanCache wxCleanCache;

	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxArmyService, WxArmy.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxArmy army) {
		if(army==null) {
			return Result.error("部队信息为空");
		}
		if(StringUtils.isEmpty(army.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxArmyService, WxArmy.class, new String[] {"armyCode","armyName"}, "keyId", army, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("部队番号和名称不能重复");
			}
			iWxArmyService.save(army);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxArmyService, WxArmy.class, new String[] {"armyCode","armyName"}, "keyId", null, army, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("部队番号和名称不能重复");
			}
			iWxArmyService.updateById(army);
		}
		
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("传入参数不完整，不能删除");
		}
		iWxArmyService.removeById(keyId);
		//删除缓存
		wxCleanCache.cleanArmyCache();
		return Result.ok();
	}
}

